
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QTime>

#include "core/engine.h"
#include "core/game.h"
#include "ui/hud.h"

Hud::Hud(QWidget* pkParent)
    : QWidget(pkParent),
      m_ratio(0.35),
      m_kBackground(":/image/board/hud.png")
{
    Q_ASSERT(!m_kBackground.isNull());
}

Hud::~Hud()
{

}

void Hud::paintEvent(QPaintEvent* pkEvent)
{
    // Propagate.
    QWidget::paintEvent(pkEvent);

    // Base scaling on background pixmap.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());

    // Paint.
    QPainter kPainter(this);

    // Paint the background.
    QSize kScaledBackgroundSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kBackgroundOffset((width() - kScaledBackgroundSize.width()) / 2, 0);

    kPainter.drawPixmap(QRectF(kBackgroundOffset.x(), kBackgroundOffset.y(), kScaledBackgroundSize.width(), kScaledBackgroundSize.height()), m_kBackground, m_kBackground.rect());

    // Paint the text.
    kPainter.setFont(QFont("Wortellina", kScaledBackgroundSize.height() / 8));

    int iElapsed = Engine::instance().game().elapsed();
    int iShots = Engine::instance().game().shots();

    kPainter.drawText(kBackgroundOffset.x(), kBackgroundOffset.y(), kScaledBackgroundSize.width(), kScaledBackgroundSize.height(), Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap, trUtf8("Time:%1\tShots:%2").arg(iElapsed).arg(iShots));
}
